<template>
  <div class="download-error-dialog">
    <el-dialog
      v-model="dialogVisible"
      width="400"
      :modal="false"
      align-center
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :destroy-on-close="true"
      @closed="closedHandle"
    >
      <template #header>
        <dialog-title title="提示" />
      </template>
      <div class="content">
        <div class="content-unit">
          <span class="content-title">文件名:</span>
          <span>{{ title }}</span>
        </div>
        <div class="content-unit">
          下载失败
        </div>
        <div
          class="content-unit"
          style="margin-top:20px;"
        >
          <span class="content-title">失败原因:</span>
        </div>
        <div class="content-unit">
          {{ errorMsg }}
        </div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <div
            class="dialog-footer-service"
            @click="showService"
          >
            <svg-icon
              icon-class="custom-service"
              :size="20"
            />
            <span>人工客服</span>
          </div>
          <el-button
            type="default"
            class="button"
            @click="handleCancelInner('cancel')"
          >
            取消
          </el-button>
          <el-button
            type="primary"
            class="button"
            @click="handleCancelInner('cancel')"
          >
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import useDialogPromiseHook from '@/hooks/use-dialog-promise-hook'

const dialog = useDialogPromiseHook()

const showService = () => {
  dialog.showDialog('dialogQr')
      .then(() => {})
      .catch(() => {})
}

const props = defineProps({
  handleSuccess: {
    type: Function,
    default:()=>{}
  },
  handleCancel: {
    type: Function,
    default:()=>{}
  },
  title:{
    type: String,
    default:'',
  },
  errorMsg:{
    type: String,
    default:'',
  }
})

const dialogVisible = ref(true)
const clickType = ref('cancel')
const cancelType = ref('cancel')

const handleSuccessInner = () => {
  clickType.value = 'success'
  dialogVisible.value = false
}

const handleCancelInner = (_cancelType: string) => {
  clickType.value = 'cancel'
  cancelType.value = _cancelType
  dialogVisible.value = false
}

const closedHandle = () => {
  if (clickType.value === 'success') {
    props.handleSuccess()
  } else {
    props.handleCancel(cancelType.value)
  }
}
</script>

<style scoped lang="scss">
.download-error-dialog{
  .content{
    background: rgba(25,38,85,0.05);
    border-radius: 6px;
    padding:14px;
    
    font-family: Microsoft YaHei, Microsoft YaHei;
    font-weight: 400;
    font-size: 12px;
    color: #3B3D60;
    .content-unit{
      margin:8px 0;
      .content-title{
        font-weight: bold;
      }
    }
  }
  
  .dialog-footer{
    display: flex;
    align-items: center;
    justify-content: space-between;
    .dialog-footer-service{
      flex-grow: 1;
      display: flex;
      align-items: center;
      justify-content: flex-start;
      gap:5px;
      font-size:12px;
      cursor:pointer;
    }
  }
}
</style>
